/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024.
 * Description: header for virtual address domain isolation mmu context
 * Author: zhuyan
 * Create: 2024-02-07
 */
#ifndef RTOS_ISOLATION_VM_CONTEXT_H
#define RTOS_ISOLATION_VM_CONTEXT_H
#include <linux/sched.h>
#include <linux/mm_types.h>

#ifdef CONFIG_RTOS_ISOLATION_VM
#include <asm/sysreg.h>
#include <asm/pgtable-hwdef.h>

DECLARE_STATIC_KEY_FALSE(isovm_enabled);

static inline void isovm_update_tcr_ttbr(struct mm_struct *prev, struct mm_struct *next,
					 struct task_struct *tsk)
{
	bool is_isovm = (tsk->thread.t0sz == ISOVM_T0SZ_ISOLATION);
	u64 isovm_tcr = is_isovm ? ISOVM_TCR_ISOLATION : ISOVM_TCR_NORMAL;
	u64 isovm_ttbr = is_isovm ? 0 : TTBR_SEC_MASK;

	if (next == &init_mm || !static_branch_unlikely(&isovm_enabled))
		return;
	sysreg_clear_set(tcr_el1, ISOVM_TCR_MASK, isovm_tcr);
	/*
	 * update the ttbr1 when switching from one task to another in the
	 * same process.
	 * when tasks are switched between different processes, we update the
	 * ttbr1 later in cpu_do_switch_mm().
	 */
	if (prev == next)
		sysreg_clear_set(ttbr1_el1, TTBR_SEC_MASK, isovm_ttbr);
	isb();
}
#else
static inline void isovm_update_tcr_ttbr(struct mm_struct *prev, struct mm_struct *next,
					 struct task_struct *tsk) {}
#endif

#endif
